package com.xxx.kjds.controller;

import com.xxx.kjds.entity.FinanceCheck;
import com.xxx.kjds.entity.TraceKey;
import com.xxx.kjds.entity.User;
import com.xxx.kjds.entity.Wallets;
import com.xxx.kjds.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员控制器
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private FinanceService financeService;
    @Autowired
    private TraceService traceService;
    @Autowired
    private WalletService walletService;

    /**
     * 根据id查询管理员
     * @param request
     * @return 管理员信息
     */
    @PostMapping(value = "/getInfo")
    @ResponseBody
    public User findById(HttpServletRequest request) {
        String str_id = request.getParameter("id");
        Integer int_id = Integer.valueOf(str_id);
        return userService.selectByPrimaryKey(int_id);
    }

//    // 查询finance、trace、wallet三表，组装数据完整交易记录
//    @RequestMapping(value = "/getFinList", method = RequestMethod.POST)
//    @ResponseBody
//    public List<Object> getFinList(HttpServletResponse response) {
//        List<Object> resultList = new ArrayList<>();
//        List<FinanceCheck> financeCheckList = financeService.getList();
//        List<TraceKey> traceList = new ArrayList<>();
//        for (FinanceCheck financeCheck : financeCheckList) {
//            TraceKey tk = traceService.selectByFid(financeCheck.getId());
//            Wallets wallets = walletService.selectByPrimaryKey(tk.getWalletsId());
//            resultList.add(financeCheck);
//            resultList.add(wallets);
//        }
//        return resultList;
//    }

    /**
     * 修改管理员信息
     * @param request
     * @return 是否修改成功
     */
    @PostMapping(value = "/updateInfo")
    @ResponseBody
    public String updateInfo(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("id"));
        User user = userService.selectByPrimaryKey(uid);

        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        if (email.length() > 30) {
            return "false";
        }
        if (phone.length() > 11) {
            return "false";
        }
        user.setEmail(email);
        user.setPhone(phone);

        int result = userService.updateByPrimaryKey(user);
        if (result > 0) {
            return "true";
        }
        return "false";
    }

    /**
     * 修改管理员密码
     * @param request
     * @return 是否修改成功
     */
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    public String updatePwd(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("id"));
        User user = userService.selectByPrimaryKey(uid);

        String opw = request.getParameter("opw");
        String npw1 = request.getParameter("npw1");
        String npw2 = request.getParameter("npw2");

        //对输入判断
        if (!opw.equals(user.getPassword())) {
            return "输入的旧密码不正确，请重新输入！";
        } else if (!npw1.equals(npw2)) {
            return "输入的两次密码不一致,请重新输入!";
        }

        user.setPassword(npw1);
        int result = userService.updateByPrimaryKey(user);
        if (result > 0) {
            return "修改密码成功!";
        }
        return "修改密码成功!";
    }
}
